package jwdt.xadmin.work.action;

import jwdt.common.action.BaseAction;
import jwdt.common.util.NewObjectUtil;
import jwdt.xadmin.system.bean.TUser;
import jwdt.xadmin.work.bean.TEmail;
import jwdt.xadmin.work.bean.TEmailSet;
import jwdt.xadmin.work.service.EmailManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.StringUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller("/xadmin/work")
public class EmailManageAction extends BaseAction {

	private EmailManageService emailManageService = (EmailManageService)app.getBean("emailManageService");
	
	@RequestMapping("/listInEmail")
	public Page doListInEmail(Form f, Module m) throws Exception {
		PageUtil pu = (PageUtil)f.copy2Class(PageUtil.class);
		TUser userInfo = (TUser) f.getSession("userInfo");
		String zt = f.getString("zt", "0");
    	return m.getWritePage(f, emailManageService.doInList(pu,userInfo,zt).toJson());
	}
	
	@RequestMapping("/listOutEmail")
	public Page doListOutEmail(Form f, Module m) throws Exception {
		PageUtil pu = (PageUtil)f.copy2Class(PageUtil.class);
		TUser userInfo = (TUser) f.getSession("userInfo");
		String zt = f.getString("zt", "0");
    	return m.getWritePage(f, emailManageService.doOutList(pu,userInfo,zt).toJson());
	}
	
	@RequestMapping("/emailLeftTree")
	public Page doLeftEmailTree(Form f, Module m) throws Exception {
    	return m.getJsonPage(f, emailManageService.doLeftTree());
	}

	@RequestMapping("/emailSet")
	public Page doEmailSet(Form f, Module m) throws Exception {
		TUser user = (TUser) f.getSession("userInfo");
		TEmailSet es = (TEmailSet)Jdb.get().get(TEmailSet.class, user.getUserid());
		if(es == null) {
			es = (TEmailSet)NewObjectUtil.getNewObject(TEmailSet.class);
		}
		f.add("obj", es);
    	return m.findPage("/xadmin/work/emailSet");
	}

	@RequestMapping("/emailSetAct")
	public Page doEmailSetAct(Form f, Module m) throws Exception {
		TUser user = (TUser) f.getSession("userInfo");
		TEmailSet es = (TEmailSet) f.copy2Class(TEmailSet.class);
		es.setUserid(user.getUserid());
		String ret = "设置成功";
		try {
			if(StringUtil.nullOrBlank(es.getUserid())) {//新增
				es.setUserid(user.getUserid());
				Jdb.get().add(es);
			} else {
				es.setUserid(user.getUserid());
				Jdb.get().update(es);
			}
		} catch(Exception e) {
			e.printStackTrace();
			ret = "设置出错";
		}
		return m.getWritePage(f, ret);
	}

	@RequestMapping("/addEmail")
	public Page doAddact(Form f, Module m) throws Exception {
		TUser userInfo = (TUser) f.getSession("userInfo");
		TEmail obj = (TEmail)f.copy2Class(TEmail.class);
		int zt = f.getInt("zt");
		int oi = f.getInt("oldemailid");
		return m.getWritePage(f, emailManageService.doAddact(obj,userInfo,zt, oi));
	}
	
	@RequestMapping("/editEmail")
	public Page doEditact(Form f, Module m) throws Exception {
		int emailid = f.getInt("emailid");
		TEmail obj = (TEmail)f.copy2Class(TEmail.class);
		int zt = f.getInt("zt");
		return m.getWritePage(f, emailManageService.doEditact(obj, emailid,zt));
	}
	
	@RequestMapping("/delInboxEmail")
	public Page doInboxDel(Form f, Module m) throws Exception {
		int emailid = f.getInt("emailid");
		TUser userInfo = (TUser) f.getSession("userInfo");
		return m.getWritePage(f, emailManageService.doUpdateEmailState(emailid,userInfo.getUserid(),"deleted"));
	}
	
	@RequestMapping("/delDeletedEmail")
	public Page doDeletedDel(Form f, Module m) throws Exception {
		int emailid = f.getInt("emailid");
		TUser userInfo = (TUser) f.getSession("userInfo");
		return m.getWritePage(f, emailManageService.doUpdateEmailState(emailid,userInfo.getUserid(),"delete"));
	}
	
	@RequestMapping("/redoEmail")
	public Page doRedoEmail(Form f, Module m) throws Exception {
		int emailid = f.getInt("emailid");
		TUser userInfo = (TUser) f.getSession("userInfo");
		return m.getWritePage(f, emailManageService.doUpdateEmailState(emailid,userInfo.getUserid(),"inbox"));
	}
	
	@RequestMapping("/delEmail")
	public Page doDel(Form f, Module m) throws Exception {
		int emailid = f.getInt("emailid");
		TUser userInfo = (TUser) f.getSession("userInfo");
		return m.getWritePage(f, emailManageService.doDelact(emailid,userInfo));
	}
	
	@RequestMapping("/emailWindow")
	public Page doLoad(Form f, Module m) throws Exception {
		int emailid = f.getInt("emailid");
		int realEamil = f.getInt("realEmail");
		TUser userInfo = (TUser) f.getSession("userInfo");
		if(realEamil == 1){
			f.add("realEamil", emailManageService.doLoadReceiver(emailid,userInfo));
		}
		emailManageService.doUpdateEmailState(emailid, userInfo.getUserid(), "read");
		f.add("obj", emailManageService.doLoad(emailid));
		return m.findPage("/xadmin/work/emailWindow");
	}
	
	@RequestMapping("/emailStateWindow")
	public Page doEmailStateWindow(Form f, Module m) throws Exception {
		f.add("userInfo", (TUser) f.getSession("userInfo"));
		int emailid = f.getInt("emailid");
		f.add("emailid", emailid);
		return m.findPage("/xadmin/work/emailStateWindow");
	}
	
	@RequestMapping("/listUserEmailState")
	public Page doListUserEmailState(Form f, Module m) throws Exception {
		PageUtil pu = (PageUtil)f.copy2Class(PageUtil.class);
		int emailid = f.getInt("emailid");
		return m.getWritePage(f, emailManageService.doListUserEmailState(pu,emailid,"").toJson());
	}
	
	@RequestMapping("/getNewEmail")
	public Page doGetNewEmail(Form f, Module m) throws Exception {
		TUser userInfo = (TUser) f.getSession("userInfo");
		return m.getWritePage(f, emailManageService.doGetNewEmail(userInfo));
	}
	
	@RequestMapping("/emailReplyWindow")
	public Page doReplyWindow(Form f, Module m) throws Exception {
		int emailid = f.getInt("emailid");
		TUser userInfo = (TUser) f.getSession("userInfo");
		emailManageService.doUpdateEmailState(emailid, userInfo.getUserid(), "read");
		TEmail email = (TEmail)Jdb.get().get(TEmail.class, emailid);
		email.setContent(email.getContent().replaceAll("\r\n", "%OD%OA"));
		f.add("obj", email);
		return m.findPage("/xadmin/work/emailReplyWindow");
	}
	
	@RequestMapping("/sendRealEmail")
	public Page doSendRealEmail(Form f, Module m) throws Exception {
		TEmail obj = (TEmail)f.copy2Class(TEmail.class);
		TUser userInfo = (TUser) f.getSession("userInfo");
		String receiver = f.getString("cc");
		return m.getWritePage(f, emailManageService.doSendRealEmail(obj,userInfo,receiver));
	}
	
}
